package com.hm.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.cms.mongo.domain.CmsArticleContent;
import com.hm.cms.mongo.domain.QCmsArticleContent;
import com.hm.cms.mongo.vo.CmsArticleContentVo;
import com.hm.cms.mongo.vo.CmsColumnVo;
import com.hm.cms.share.repository.mongo.CmsArticleContentRepository;
import com.hm.cms.share.service.CmsArticleContentService;
import com.hm.cms.share.service.CmsColumnService;
import com.hm.common.common.BeanUtil;
import com.hm.common.common.CommonUtil;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.PageInfo;
import com.mysema.query.types.expr.BooleanExpression;

/**
 * @author shishun.wang
 * @date 下午2:39:17 2016年8月9日
 * @version 1.0
 * @describe
 */
@Service
public class CmsArticleContentServiceImpl implements CmsArticleContentService {

	@Autowired
	private CmsArticleContentRepository cmsArticleContentRepository;
	
	@Autowired
	private CmsColumnService cmsColumnService;

	@Override
	public CmsArticleContentVo getCmsArticleContent(String id) {
		CmsArticleContent articleContent = cmsArticleContentRepository.findOne(id);
		if (null == articleContent) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		CmsArticleContentVo articleContentVo = (CmsArticleContentVo) BeanUtil.copyProperties(new CmsArticleContentVo(), articleContent);
		articleContentVo.setColumnName("未知");
		if(CommonUtil.isNotEmpty(articleContentVo.getColumn())){
			CmsColumnVo cmsColumn = cmsColumnService.getCmsColumn(articleContentVo.getColumn());
			if(null != cmsColumn){
				articleContentVo.setColumnName(cmsColumn.getName());
			}
		}

		return articleContentVo;
	}

	@Override
	public String addCmsArticleContent(CmsArticleContentVo articleContentVo, String operationUser) {
		CmsArticleContent articleContent = new CmsArticleContent();
		{
			articleContent = (CmsArticleContent) BeanUtil.copyProperties(new CmsArticleContent(), articleContentVo);
			articleContent.setCreateTime(System.currentTimeMillis());
			articleContent.setCreateUser(operationUser);
		}

		return cmsArticleContentRepository.save(articleContent).getId();
	}

	@Override
	public String updateCmsArticleContent(CmsArticleContentVo articleContentVo, String operationUser) {
		CmsArticleContent articleContent = cmsArticleContentRepository.findOne(articleContentVo.getId());
		if (null == articleContent) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		articleContent = (CmsArticleContent) BeanUtil.copyProperties(articleContent, articleContentVo);

		return cmsArticleContentRepository.save(articleContent).getId();
	}

	@Override
	public String deleteCmsArticleContent(CmsArticleContentVo articleContentVo, String operationUser) {
		cmsArticleContentRepository.delete(articleContentVo.getId());
		return articleContentVo.getId();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<CmsArticleContentVo> queryCmsArticleContent(String weibsite, String column, Integer pageNumber,
			Integer pageSize) {
		PageInfo<CmsArticleContentVo> pageInfo = new PageInfo<CmsArticleContentVo>(pageNumber, pageSize);
		if (CommonUtil.isEmpty(weibsite, column)) {
			return pageInfo;
		}
		QCmsArticleContent qCmsArticleContent = QCmsArticleContent.cmsArticleContent;

		BooleanExpression predicate = qCmsArticleContent.weibsite.eq(weibsite)
				.and(qCmsArticleContent.column.eq(column));

		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				qCmsArticleContent.createTime.desc());
		Page page = cmsArticleContentRepository.findAll(predicate, pageable);

		List<CmsArticleContentVo> vos = new ArrayList<CmsArticleContentVo>();
		page.getContent().forEach(baseInfo -> {
			CmsArticleContentVo articleContentVo = (CmsArticleContentVo) BeanUtil.copyProperties(new CmsArticleContentVo(), baseInfo);
			articleContentVo.setColumnName("未知");
			if(CommonUtil.isNotEmpty(articleContentVo.getColumn())){
				CmsColumnVo cmsColumn = cmsColumnService.getCmsColumn(articleContentVo.getColumn());
				if(null != cmsColumn){
					articleContentVo.setColumnName(cmsColumn.getName());
				}
			}
			vos.add(articleContentVo);
		});

		return pageInfo.execute(page, vos);
	}

}
